//
//  DMMyOrderDetailVC.m
//  DistributionMall
//
//  Created by JHnoplan on 2018/10/18.
//  Copyright © 2018年 JHnoplan. All rights reserved.
//

#import "DMMyOrderDetailVC.h"
#import "DMMyOrderDetailHeadCell.h"
#import "DMMyOrderDetailAddressCell.h"
#import "DMConfirmOrderSecondCell.h"
#import "DMMyOrderDetailBottomCell.h"
#import "DMMyOrderLogisticsVC.h"
#import "DMMyOrderCommentVC.h"
#import "DMSelectPayWayVC.h"
#import "DMMyOrderLogisticsVC.h"

//view
#import "DMAlertView.h"

//request
#import "KStoreRequest.h"
//other
#import "NSDate+Util.h"

@interface DMMyOrderDetailVC ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet UIButton *firstBt;
@property (weak, nonatomic) IBOutlet UIButton *secondBt;
@property (weak, nonatomic) IBOutlet UIButton *thirdBt;
@property (weak, nonatomic) IBOutlet UIView *bottomView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomViewHeight;




@property (nonatomic,strong) KDisOrderDetailModel * detailModel;

@end

@implementation DMMyOrderDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self startInit];
    [self getData];
}

-(void)startInit {
    self.navigationItem.title = @"订单详情";
    self.view.backgroundColor = DMDefaultColor;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"DMMyOrderDetailHeadCell" bundle:nil] forCellReuseIdentifier:@"DMMyOrderDetailHeadCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"DMMyOrderDetailAddressCell" bundle:nil] forCellReuseIdentifier:@"DMMyOrderDetailAddressCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"DMConfirmOrderSecondCell" bundle:nil] forCellReuseIdentifier:@"DMConfirmOrderSecondCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"DMMyOrderDetailBottomCell" bundle:nil] forCellReuseIdentifier:@"DMMyOrderDetailBottomCell"];
    
    _firstBt.layer.borderWidth = 1;
    _firstBt.layer.borderColor = DMMainColor.CGColor;
    _secondBt.layer.borderWidth = 1;
    _secondBt.layer.borderColor = RGBCOLOR(121, 121, 121).CGColor;
    _thirdBt.layer.borderWidth = 1;
    _thirdBt.layer.borderColor = RGBCOLOR(121, 121, 121).CGColor;
    
    //待付款
    if ([self.model.soStatus isEqualToString:@"awaiting_pay"]) {
        [self.firstBt setTitle:@"去支付" forState:UIControlStateNormal];
        [self.secondBt setTitle:@"取消订单" forState:UIControlStateNormal];
        self.thirdBt.hidden = YES;
        
        
        //待发货
    }else if ([self.model.soStatus isEqualToString:@"awaiting_ship"]) {
        self.thirdBt.hidden = YES;
        self.secondBt.hidden = YES;
        
        self.firstBt.hidden = YES;
        self.bottomView.hidden = YES;
        self.bottomViewHeight.constant = 0;
        
        
        //待收货
    }else if ([self.model.soStatus isEqualToString:@"shipped"]){
        
        [self.firstBt setTitle:@"确认收货" forState:UIControlStateNormal];
        [self.secondBt setTitle:@"查看物流" forState:UIControlStateNormal];
        self.thirdBt.hidden = YES;
        
        
        //待评价
    }else if ([self.model.soStatus isEqualToString:@"completed"]){
        [self.firstBt setTitle:@"去评价" forState:UIControlStateNormal];
        [self.secondBt setTitle:@"查看物流" forState:UIControlStateNormal];
        [self.thirdBt setTitle:@"删除订单" forState:UIControlStateNormal];
        
        if (self.model.soItemViews.count>0) {
            
            KDisOrderItemModel * orderItemModel = self.model.soItemViews[0];
            if ([orderItemModel.isReview isEqualToString:@"N"]) {
                self.firstBt.hidden = NO;
                
            }else{
                self.firstBt.hidden = YES;
            }
        }
        
    }
}

#pragma mark ----netWork----

-(void)getData{
    KStoreRequest * request = [[KStoreRequest alloc]initStoreOrderDetailWithKey:self.model.id];
    
    [JLProgressHUDManager showLoadingHud];
    MJWeakSelf
    [request newStartRequestWithCompletion:^(KBaseModel *baseModel) {
        
        [JLProgressHUDManager hideHud];
        KDisOrderDetailModel * detailModel = [KDisOrderDetailModel modelWithJSON:baseModel.data];
        weakSelf.detailModel = detailModel;
        [weakSelf.tableView reloadData];
    } failure:^(KBaseModel *errorModel) {
        
        [JLProgressHUDManager showErrorHud:errorModel.msg];
    }];
    
}

-(void)deleteOrder{
    
    
    KStoreRequest * request = [[KStoreRequest alloc]initStoreCancelOrderWithKey:self.model.id];
    
    [JLProgressHUDManager showLoadingHud];
    MJWeakSelf
    [request newStartRequestWithCompletion:^(KBaseModel *baseModel) {
        
        [JLProgressHUDManager showSuccessHud:@"取消订单成功"];
        [weakSelf.navigationController popViewControllerAnimated:YES];
        
    } failure:^(KBaseModel *errorModel) {
        
        [JLProgressHUDManager showErrorHud:errorModel.msg];
        
    }];
}


- (IBAction)buttonClick:(UIButton *)sender {
    if ([sender.titleLabel.text isEqualToString:@"取消订单"]||[sender.titleLabel.text isEqualToString:@"删除订单"]) {
        DMAlertView *alertView = [[DMAlertView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        [self.navigationController.view addSubview:alertView];
        [alertView showAlertViewWithTitle:@"您确认取消该订单吗" status:2];
        MJWeakSelf
        [alertView setSureBlock:^{
            [weakSelf deleteOrder];
        }];
    }else if ([sender.titleLabel.text isEqualToString:@"去支付"]) {
        
        DMSelectPayWayVC * vc = [[DMSelectPayWayVC alloc]init];
        vc.entityid = self.model.id;
        vc.payPrice = self.model.total.doubleValue;
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if ([sender.titleLabel.text isEqualToString:@"查看物流"]) {
        DMMyOrderLogisticsVC * vc = [[DMMyOrderLogisticsVC alloc]init];
        vc.soEntityId = self.model.id;
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([sender.titleLabel.text isEqualToString:@"确认收货"]) {
        KStoreRequest * request = [[KStoreRequest alloc]initStoreConfirmReceiptWithKey:self.model.id];
        MJWeakSelf
        [request newStartRequestWithCompletion:^(KBaseModel *baseModel) {
            
            [JLProgressHUDManager showSuccessHud:@"已确认收货"];
            
            [weakSelf.navigationController popViewControllerAnimated:YES];
            
            
        } failure:^(KBaseModel *errorModel) {
            [JLProgressHUDManager showErrorHud:errorModel.msg];
        }];
        
        
    }else if ([sender.titleLabel.text isEqualToString:@"去评价"]) {
        DMMyOrderCommentVC *vc = [DMMyOrderCommentVC new];
        vc.model = self.model;
        MJWeakSelf
        [vc setReloadBlock:^{
            [weakSelf getData];
        }];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - tableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 8;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 3) {
        return 80;
    }
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 70;
    }
    if (indexPath.section == 2) {
        return 138;
    }else if (indexPath.section == 3) {
        return 212;
    }
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 100;
    return self.tableView.rowHeight;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        DMMyOrderDetailHeadCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DMMyOrderDetailHeadCell" forIndexPath:indexPath];
        if (cell == nil) {
            cell = [[NSBundle mainBundle]loadNibNamed:@"DMMyOrderDetailHeadCell" owner:self options:nil].firstObject;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if ([self.model.soStatus isEqualToString:@"awaiting_pay"]) {
            cell.waitView.hidden =NO;
            cell.contentLb.text = @"";
        }else{
            cell.waitView.hidden =YES;
            cell.contentLb.text = @"";
        }

        return cell;
    }else if (indexPath.section == 1) {
        DMMyOrderDetailAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DMMyOrderDetailAddressCell" forIndexPath:indexPath];
        if (cell == nil) {
            cell = [[NSBundle mainBundle]loadNibNamed:@"DMMyOrderDetailAddressCell" owner:self options:nil].firstObject;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        KStoreAddressModel * addressModel =self.detailModel.soAddrView;
        cell.nameLb.text =  addressModel.person;
        cell.phoneLb.text = addressModel.mobile;
        cell.addressLb.text = [NSString stringWithFormat:@"%@%@%@%@",addressModel.prName,addressModel.ciName,addressModel.arName,addressModel.addr];
        return cell;
    }else if (indexPath.section == 2) {
        DMConfirmOrderSecondCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DMConfirmOrderSecondCell" forIndexPath:indexPath];
        if (cell == nil) {
            cell = [[NSBundle mainBundle]loadNibNamed:@"DMConfirmOrderSecondCell" owner:self options:nil].firstObject;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        if (self.detailModel.soEntityView.soItemViews.count>0) {
            KDisOrderItemModel * itemModel = self.detailModel.soEntityView.soItemViews[0];
            cell.nameLb.text = itemModel.name;
            
            [cell.headImage sd_setImageWithURL:[NSURL URLWithString:itemModel.mainCloudPath] placeholderImage:[UIImage imageNamed:@"moren_touxiang"]];
            cell.sizeLb.text = [NSString stringWithFormat:@"规格:%@",itemModel.attrOpValues];
            cell.numLb.text = [NSString stringWithFormat:@"数量：%@件",itemModel.count];
        }
        
        return cell;
    }
    DMMyOrderDetailBottomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DMMyOrderDetailBottomCell" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"DMMyOrderDetailBottomCell" owner:self options:nil].firstObject;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.priceLb.text =[NSString stringWithFormat:@"¥%@",self.detailModel.soEntityView.total];
    cell.dateLb.text = [NSDate dateWithStrTimeInterval:self.detailModel.soEntityView.createTime fromatStr:@"yyyy-MM-dd hh:mm:ss"];

    cell.numLb.text = self.detailModel.soEntityView.soNo;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 0) {
        DMMyOrderLogisticsVC *vc = [DMMyOrderLogisticsVC new];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

@end
